package run;

import com.Neo4jSpringbootApp;
import com.domain.CityModel;
import com.domain.DomainOne;
import com.domain.JobRelation;
import com.domain.PersonModel;
import com.repository.CityModelRepository;
import com.repository.DomainOneRepository;
import com.repository.JobRelationRepository;
import com.repository.PersonModelRepository;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import utils.Utils;

import java.util.ArrayList;
import java.util.Optional;

@Slf4j
@SpringBootTest(classes = {Neo4jForSpringBoot.Dummy.class},webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class Neo4jForSpringBoot {

    @Configuration
    @Import(value ={Neo4jForSpringBoot.class })
    @ImportAutoConfiguration(value = {Neo4jSpringbootApp.class})
    public class Dummy{

    }
    @Autowired
    GenericApplicationContext applicationContext;
    @Test
    public void cityModelQueryTest(){
        CityModelRepository bean = applicationContext.getBean(CityModelRepository.class);

        bean.findById(2l).ifPresent(tmp->{
           assert tmp.getName().equals("上海");

       });

        CityModel cityModel = bean.findOne(Example.of(CityModel.builder().name("上海").build())).orElse(null);
        assert cityModel!=null;

        CityModel cityModel2 = bean.findOne(Example.of(CityModel.builder().jobRelations(
                Lists.newArrayList(JobRelation.builder().name("java软件工程师").build())
        ).build())).orElse(null);
        assert cityModel2!=null;

    }
    @Test
    public void deleteAllFromGraph(){
        CityModelRepository bean = applicationContext.getBean(CityModelRepository.class);
        bean.deleteAll();;
        PersonModelRepository bean2 = applicationContext.getBean(PersonModelRepository.class);
        bean2.deleteAll();;

    }
    @Test
    public void cityModelSaveTest(){
        Neo4jTemplate template = applicationContext.getBean(Neo4jTemplate.class);

        // delete model .
        CityModelRepository bean = applicationContext.getBean(CityModelRepository.class);
        PersonModel mayor = PersonModel.builder().name("市长1").age(50).farvoriteFood("鱼香豆腐").build();
        PersonModel build = PersonModel.builder().name("市民2").age(60).farvoriteFood("青菜").build();
        PersonModel build1 = PersonModel.builder().name("市民3").age(20).farvoriteFood("kfc").build();
        PersonModel build2 = PersonModel.builder().name("市民4").age(30).farvoriteFood("猪肉").build();
        PersonModel build3 = PersonModel.builder().name("市民5").age(40).farvoriteFood("羊肉").build();


        CityModel cityModel = CityModel.builder().personModels(Lists.newArrayList(
                mayor, build
                , build1
                , build2
                , build3

        )).
                mayor(mayor).
                jobRelations(
                        Lists.newArrayList(
                                JobRelation.builder().name("市长").person(mayor).build(),
                                JobRelation.builder().name("java软件工程师").person(build2).build(),
                                JobRelation.builder().name("测试工程师").person(build3).build(),
                                JobRelation.builder().name("运维工程师").person(build1).build(),
                                JobRelation.builder().name("后勤").person(build).build()
                        )
                ).exoticProperty("亚洲，非洲，欧洲，美州").name("上海")
                .build();

        bean.save(cityModel);
        log.info("{}",cityModel.toString());

    }

    @Test
    public void domainOneTest(){
        Utils.print(applicationContext);

        DomainOneRepository bean = applicationContext.getBean(DomainOneRepository.class);

        ArrayList<DomainOne> domainOnes = Lists.newArrayList(DomainOne.builder().attrs("attr1").build(), DomainOne.builder().attrs("attr2").build());

        bean.saveAll(domainOnes);

        log.info(domainOnes.get(0).toString());;
        log.info(domainOnes.get(1).toString());;

    }
}
